#include<iostream>
#include<vector>
using namespace std;
const int N = 105;

int n, q, dp[N][N];
vector<pair<int, int> > tr[N];

int dfs(int u, int fa) {
    int cnt = 0;
    for(auto[v, w] : tr[u]) {
        if(v == fa) continue;
        int child = dfs(v, u);
        cnt += child + 1;
        for(int i=min(cnt, q); i>=1; i--) {
            for(int j=1; j<=min(child+1, i); j++) {
                dp[u][i] = max(dp[u][i], dp[u][i-j] + dp[v][j-1] + w);
            }
        }
    }
    return cnt;
}

int main() {
    cin >> n >> q;
    for(int i=1; i<n; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        tr[u].push_back({v, w});
        tr[v].push_back({u, w});
    }
    dfs(1, -1);
    cout << dp[1][q];
    return 0;
} 